import { getDevInfo, getDevDetail, getGpsIntvTime, getHistoryPath } from '../services/api';
import { gcjEncryptta, bdEncrypt } from '../utils/gps';

const randomPosition = array => ({
  longitude: array[0] + 0.05 - Math.random() * 0.1,
  latitude: array[1] + 0.05 - Math.random() * 0.1,
});

export default {
  namespace: 'traveltrackbaidu',

  state: {
    tags: [],
    devNum: 0,
    onDevNum: 0,
    weekOffDevNum: 0,
    yearOffDevNum: 0,
    devinfo: {},
    devList: [],
    markPosition: { lng: 114.122695, lat: 22.615686 },
  },

  effects: {
    *fetchDevList({ payload }, { call, put }) {
      const response = yield call(getDevInfo);
      if (response) {
        const markerarray = response.devInfos; // .filter(item => Object.keys(item.gpsPos).length !== 0)
        yield put({
          type: 'saveDevList',
          payload: markerarray,
        });
      }
    },

    *fetchmarkerinfo({ payload, callback }, { call, put }) {
      const response = yield call(getDevDetail, payload);
      const markPosition = { lng: 114.122695, lat: 22.615686 };
      if (response) {
        const devInfo = response.devInfoDet;
        if (response.devInfoDet.longi !== '' && response.devInfoDet.lati !== '') {
          markPosition.lng = response.devInfoDet.longi;
          markPosition.lat = response.devInfoDet.lati;
        }
        // 转换坐标百度坐标
        const temp = gcjEncryptta(markPosition.lat, markPosition.lng);
        //console.log('gdmarkPosition------------------>', temp);
        const bdmarkPosition = bdEncrypt(temp.lat, temp.lng);
        //console.log('bdmarkPosition------------------>', bdmarkPosition);
        yield put({
          type: 'updateDevInfo',
          payload: { devInfo, bdmarkPosition },
        });
      }
      if (callback) callback(markPosition);
    },

    *getGpsParam({ payload, callback }, { call, put }) {
      //console.log('getGpsParam payload------------------>', payload);
      const response = yield call(getGpsIntvTime, payload);
      let intervalValue = 10;
      if (response) {
        intervalValue = response.getParamInfo.paramVal;
      }

      if (callback) callback(intervalValue);
    },
    *fetchhistorypath({ payload, callback }, { call, put }) {
      //console.log('search path payload------------------>', payload);
      const response = yield call(getHistoryPath, payload);
      let gpss = response.devGpsTrack.map(item => item.gpsPos);
      if (gpss) {
        gpss = gpss.filter(item => item.lng !== '');
      } else {
        gpss = [];
      }
      // 转换坐标
      const pathArr = [];
      if (gpss.length > 0) {
        gpss.forEach(item => {
          const gcj = gcjEncryptta(item.lat, item.lng);
          pathArr.push(bdEncrypt(gcj.lat, gcj.lng));
        });
      }
      // const path = JSON.stringify(gpss);
      const path = JSON.stringify(pathArr);
      // //console.log(' baidu gpss  path result------------------>', path);
      if (callback) callback(path);
    },
  },

  reducers: {
    saveTags(state, action) {
      return {
        ...state,
        tags: action.payload.markerarray,
        devNum: action.payload.devNum,
        onDevNum: action.payload.onDevNum,
        weekOffDevNum: action.payload.weekOffDevNum,
        yearOffDevNum: action.payload.yearOffDevNum,
      };
    },

    updateDevInfo(state, action) {
      //console.log('action.payload.markPosition----------->', action.payload.bdmarkPosition);
      return {
        ...state,
        devinfo: action.payload.devInfo,
        markPosition: action.payload.bdmarkPosition,
      };
    },
    saveDevList(state, action) {
      return {
        ...state,
        devList: action.payload,
      };
    },
  },
};
